package com.pjnkj.thunder.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjnkj.thunder.common.json.Body;
import com.pjnkj.thunder.common.json.Meta;
import com.pjnkj.thunder.entity.Invite;
import com.pjnkj.thunder.entity.User;
import com.pjnkj.thunder.service.InviteService;
import com.pjnkj.thunder.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.pjnkj.thunder.common.json.Meta.CODE_470;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pjnkj
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/thunder/invite")
public class InviteController {
    @Autowired
    InviteService inviteService;

    @Autowired
    UserService userService;

    /**
     * 删除数据
     *
     * @param invite
     */
    @PostMapping("delete")
    public Body delete(Integer id) {
        Boolean flag = inviteService.removeById(id);
        if (flag == true) {
            return Body.newInstance("删除成功");
        }
        return Body.newInstance(Meta.CODE_400, "删除失败");
    }

    /**
     * 增加数据
     *
     * @param invite
     */
    @PostMapping("create")
    public Body create(@Validated(Invite.class) @RequestBody Invite invite) {
        Boolean flag = inviteService.saveOrUpdate(invite);
        if (flag == true) {
            return Body.newInstance("保存成功");
        }
        return Body.newInstance(Meta.CODE_400, "保存失败");
    }

    /**
     * 修改数据
     *
     * @param invite
     */
    @PostMapping("update")
    public Body update(@Validated(Invite.class) @RequestBody Invite invite) {
        Boolean flag = inviteService.updateById(invite);
        if (flag == true) {
            return Body.newInstance("修改成功");
        }
        return Body.newInstance(Meta.CODE_400, "修改失败");
    }

    /**
     * 查询数据
     *
     * @param
     */
    @PostMapping("find")
    public Body find(String id) {
        Invite info = inviteService.getById(id);
        if (info != null) {
            return Body.newInstance(info);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
     * 查询数据list
     *
     * @param
     */
    @PostMapping("findlist")
    public Body findlist() {
        List<Invite> list = inviteService.list();
        if (list != null && list.size() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }

    /**
     * 分页查询数据
     *
     * @param
     */
    @PostMapping("page")
    public Body page(Integer current, Integer size) {
        Page<Invite> pg = new Page<Invite>(current, size);
        IPage<Invite> list = inviteService.page(pg);
        if (list != null && list.getSize() > 0) {
            List<Invite> records = list.getRecords();
            for (int i = 0; i < records.size(); i++) {
                Invite invite = records.get(i);
                User inviteuser = userService.getById(invite.getInviteid());
                User useruser = userService.getById(invite.getUserid());
                User parentuser = userService.getById(invite.getParentid());
                if (inviteuser != null) {
                    records.get(i).setInvite(inviteuser);
                }
                if (useruser != null) {
                    records.get(i).setUser(useruser);
                }
                if (parentuser != null) {
                    records.get(i).setParent(parentuser);
                }
            }
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }
}
